package com.lingo.ffmpegdemo.lg.utils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author watson
 * @date 2021/5/25 9:06
 * @Description: TODO
 **/
public class CompletableFutureDemo {
    public static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("!!!!!!!!!!" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
        }, threadPool);
        System.out.println(completableFuture.get() + "1111111111111");

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("!!!!!!!!!!" + Thread.currentThread().getId());
            int i = 10 / 0;
            return 5;
            //执行完成以后操作
        }, threadPool).whenComplete((res, exection) -> {
            System.out.println("KK" + res + "---------" + exection);
        }).exceptionally(throwable -> {
            //异常以后，返回默认值，异常处理
            return 10;
        });
        System.out.println(integerCompletableFuture.get());


        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("!!!!!!!!!!" + Thread.currentThread().getId());
            int i = 10 / 2;
            return 5;
            //执行完成以后操作
        }, threadPool).handle((resulr, threable) -> {

            if (resulr != null) {
                return resulr * 2;
            }
            if (threable != null) {
                System.out.println(threable);
                return 0;
            }
            return 0;
        });
        System.out.println(completableFuture1.get() + "haha");
    }
}
